<template>
  <el-dialog title="销售订单选择"
             v-if="showFlag"
             :visible.sync="showFlag"
             :modal= false
             width="80%"
             center
  >
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="100px">
      <el-row>
        <el-col :span="8">
          <el-form-item label="销售订单编码" prop="soNumber">
            <el-input
              v-model="queryParams.soNumber"
              placeholder="请输入销售订单编码"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="销售订单名称" prop="soName">
            <el-input
              v-model="queryParams.soName"
              placeholder="请输入销售订单名称"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="订单日期" prop="orderDate">
            <el-date-picker clearable
                            v-model="queryParams.orderDate"
                            type="date"
                            value-format="yyyy-MM-dd"
                            placeholder="请选择订单日期">
            </el-date-picker>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="8">
          <el-form-item label="客户名称" prop="clientName">
            <el-input
              v-model="queryParams.clientName"
              placeholder="请输入客户名称"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item>
            <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    <el-table v-loading="loading" :data="orderList">
      <el-table-column width="50" align="center">
        <template v-slot="scope">
          <el-checkbox v-model="selectedOrderIds" :label="scope.row.soId" @change="handleRowChange(scope.row)">{{""}}</el-checkbox>
        </template>
      </el-table-column>
      <el-table-column label="销售订单编号" align="center" prop="soNumber" width="200px" />
      <el-table-column label="销售订单名称" width="150px" align="center" prop="soName" :show-overflow-tooltip="true" />
      <el-table-column label="客户编码" align="center" prop="clientCode" />
      <el-table-column label="客户名称" align="center" prop="clientName" />
      <el-table-column label="总金额(元)" align="center" prop="totalAmount" />
      <el-table-column label="订单日期" align="center" prop="orderDate">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.orderDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="预计交货日期" align="center" prop="deliveryDate">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.deliveryDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="销售人员" align="center" prop="salesPerson" />
      <el-table-column label="状态" align="center" prop="status">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.status === '草稿'" effect="dark" type="info">草稿</el-tag>
          <el-tag v-else-if="scope.row.status === '已审批'" effect="dark">已审批</el-tag>
          <el-tag v-else-if="scope.row.status === '已完成'" effect="dark" type="success">已完成</el-tag>
          <el-tag v-else-if="scope.row.status === '已退货'" effect="dark" type="danger">已退货</el-tag>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="confirmSelect">确 定</el-button>
      <el-button @click="showFlag=false">取 消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { getOrder, listOrder, listStatusOrder } from "@/api/mes/xs/order";
export default {
  name: "MultiOrderSelect",
  props: {
    status: null,
    status2: null,
  },
  data() {
    return {
      showFlag: false,
      selectedOrderIds: [],
      selectedRows: [],
      // 遮罩层
      loading: true,
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 客户表格数据
      orderList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        soNumber: null,
        soName: null,
        clientName: null,
        orderDate: null,
        status: null,
        virtual: null,
      },
    };
  },
  created() {
    this.selectedOrderIds = [];
    this.selectedRows = [];
    this.getList();
  },
  watch: {
    showFlag(newVal) {
      if (newVal) {
        this.selectedOrderIds = [];
        this.selectedRows = [];
      }
    }
  },
  methods: {
    /** 查询客户列表 */
    getList() {
      this.loading = true;
      this.queryParams.status = this.status
      this.queryParams.virtual = this.status2
      listStatusOrder(this.queryParams).then(response => {
        this.orderList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // handleCurrent(row){
    //   if(row){
    //     this.selectedRows = row;
    //   }
    // },
    handleRowChange(row) {
      if (this.selectedOrderIds.includes(row.soId)) {
        this.selectedRows.push(row);
      } else {
        const index = this.selectedRows.findIndex(selectedRow => selectedRow.soId === row.soId);
        if (index !== -1) {
          this.selectedRows.splice(index, 1);
        }
      }
    },
    confirmSelect() {
      if (this.selectedOrderIds.length === 0) {
        this.$notify({
          title: '提示',
          type: 'warning',
          message: '请至少选择一条数据!'
        });
        return;
      }

      // 检查 selectedRows 中的 clientId 是否唯一
      const firstClientId = this.selectedRows[0].clientId;
      const isClientIdUnique = this.selectedRows.every(row => row.clientId === firstClientId);

      if (!isClientIdUnique) {
        this.$notify({
          title: '提示',
          type: 'warning',
          message: '所有选择的订单必须属于同一个客户!'
        });
        return;
      }

      this.$emit('onSelected', this.selectedRows);
      this.showFlag = false;
    }
  }
};
</script>
